﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CloverNet.Common.Extensions
{
    public class EnumDescriptor
    {
        public static IEnumerable<T> GetEnumList<T>()
        {
            Array array = Enum.GetValues(typeof(T));

            return Enumerable.Empty<T>().Concat(array.Cast<T>()) ;
        }

        public static MemberInfo[] GetMemberInfo<T>(T enumValue)
        {
            return enumValue.GetType().GetMembers();
        }
    }

    public static class EnumExtensions
    {
        private static Func<Enum, MemberInfo> _func = (m) => EnumDescriptor.GetMemberInfo(m).Single(n=> n.Name== m.ToString());

        public static string GetDescription(this Enum enumValue)
        {
            return enumValue.GetCustomAttribute<DescriptionAttribute>()?.Description?? string.Empty;
        }

        public static MemberInfo GetMemberInfo(this Enum enumValue)
        {
            return _func(enumValue);
        }

        public static T GetCustomAttribute<T>(this Enum enumValue) where T: Attribute
        {
            return enumValue.GetMemberInfo()?.GetCustomAttribute<T>();
        }
    }

}
